package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.CameraLocation;
import com.zpp.wedding_admin.entity.LocationLable;
import com.zpp.wedding_admin.entity.vo.CameraLocationQuery;
import com.zpp.wedding_admin.entity.vo.IDs;
import com.zpp.wedding_admin.service.ICameraLocationService;
import com.zpp.wedding_admin.service.ILocationLableService;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(description = "拍摄地点管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/location")
public class CameraLocationController {
    @Autowired
    private ICameraLocationService cameraLocationService;

    @Autowired
    private ILocationLableService locationLableService;

    @ApiOperation(value = "获取所有拍摄地点")
    @GetMapping("/getAll")
    public R getAll() {
        List<CameraLocation> cameraLocationList = cameraLocationService.list(null);
        Integer[] ids = getIds(cameraLocationList);
        Map<Integer, LocationLable> labelMap = getLabelMap(ids);
        cameraLocationList = getList(labelMap, cameraLocationList);
        return R.ok().data("locationList", cameraLocationList);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("getLocationPageVo/{currentPage}/{size}")
    public R getLocationPageVo(@PathVariable Long currentPage,
                             @PathVariable Long size) {

        Page<CameraLocation> page = new Page<CameraLocation>(currentPage, size);
        cameraLocationService.page(page, null);
        List<CameraLocation> locationList = page.getRecords();
        long total = page.getTotal();
        Integer[] ids = getIds(locationList);
        Map<Integer, LocationLable> labelMap = getLabelMap(ids);
        locationList = getList(labelMap, locationList);
        return R.ok().data("locationList", locationList).data("total", total);
    }

    @ApiOperation(value = "分页条件查询拍摄地点列表")
    @PostMapping("getLocationPageQuery/{currentPage}/{size}")
    public R getLocationPageQuery(@PathVariable Long currentPage,
                                @PathVariable Long size,
                                @RequestBody CameraLocationQuery locationQuery) {
        // RequestBody将json转换为实体类
        LambdaQueryWrapper<CameraLocation> lqw = new LambdaQueryWrapper<CameraLocation>();
        String locationName = locationQuery.getLocationName();
        lqw.like(!StringUtils.isEmpty(locationName), CameraLocation::getLocationName, locationName);
        Page<CameraLocation> page = new Page<CameraLocation>(currentPage, size);
        cameraLocationService.page(page, lqw);
        List<CameraLocation> locationList = page.getRecords();
        Integer[] ids = getIds(locationList);
        Map<Integer, LocationLable> labelMap = getLabelMap(ids);
        locationList = getList(labelMap, locationList);
        long total = page.getTotal();
        return R.ok().data("locationList", locationList).data("total", total);
    }

    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/deleteAllSelect")
    public R deleteAllSelect(@RequestBody Integer iDs[]) {
        boolean b = cameraLocationService.deleteAllSelect(iDs);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id删除一个")
    @DeleteMapping("/deleteOneById/{id}")
    public R deleteOneById(@PathVariable Integer id) {
        boolean b = cameraLocationService.removeById(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id获取拍摄地点信息")
    @GetMapping("/getOneById/{id}")
    public R getOneById(@PathVariable Integer id) {
        CameraLocation cameraLocation = cameraLocationService.getById(id);
        LocationLable label = getLabel(cameraLocation.getLableId());
        cameraLocation.setLocationLable(label);
        if ( cameraLocation != null ) return R.ok().data("location", cameraLocation);
        return R.error();
    }

    @ApiOperation(value = "更新或添加")
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody CameraLocation cameraLocation) {
        boolean b = cameraLocationService.saveOrUpdate(cameraLocation);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "模糊查询名称")
    @GetMapping("/searchByName/{locationName}")
    public R searchByName(@PathVariable String locationName) {
        LambdaQueryWrapper<CameraLocation> lqw = new LambdaQueryWrapper<CameraLocation>();
        lqw.like(CameraLocation::getLocationName, locationName);
        List<CameraLocation> locationList = cameraLocationService.list(lqw);
        Integer[] ids = getIds(locationList);
        Map<Integer, LocationLable> labelMap = getLabelMap(ids);
        locationList = getList(labelMap, locationList);
        return R.ok().data("locationList", locationList);
    }

    @ApiOperation(value = "根据id查询标签")
    public LocationLable getLabel(Integer id) {
        LocationLable locationLable = locationLableService.getById(id);
        if ( locationLable == null ) return null;
        return locationLable;
    }

    @ApiOperation(value = "根据id批量查询")
    public Map<Integer, LocationLable> getLabelMap(Integer []iDs) {
        LambdaQueryWrapper<LocationLable> lqw = new LambdaQueryWrapper<>();
        lqw.in(LocationLable::getLableId, iDs);
        List<LocationLable> lableList = locationLableService.list(lqw);
        Map<Integer, LocationLable> map = new HashMap<Integer, LocationLable>();
        for (LocationLable locationLable : lableList) {
            if ( map.containsKey(locationLable.getLableId()) ) continue;
            map.put(locationLable.getLableId(), locationLable);
        }
        return map;
    }

    @ApiOperation(value = "获取标签id数组")
    public Integer[] getIds(List<CameraLocation> locationList) {
        int size = locationList.size();
        Integer[] ids = new Integer[size];
        for ( int i = 0; i < size; i ++ ) {
            ids[i] = locationList.get(i).getLableId();
        }
        return ids;
    }

    @ApiOperation(value = "给拍摄地点数组赋值标签")
    public List<CameraLocation> getList(Map<Integer, LocationLable> map, List<CameraLocation> locationList) {
        for ( int i = 0; i < locationList.size(); i ++ ) {
            CameraLocation cameraLocation = locationList.get(i);
            Integer lableId = cameraLocation.getLableId();
            LocationLable locationLable = map.get(lableId);
            cameraLocation.setLocationLable(locationLable);
            locationList.set(i, cameraLocation);
        }
        return locationList;
    }
}
